package com.it.gw.modules.water.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.it.common.utils.StringUtils
import com.it.data.network.rsp.WaterRecordListVo
import com.it.data.network.rsp.WaterRecordVo
import com.it.gw.databinding.ItemWaterRecordBinding


class WaterRecordAdapter(
    var context: Context,
    var listener: ((WaterRecordListVo) -> Unit)? = null,
    var datas: List<WaterRecordListVo>
) : RecyclerView.Adapter<WaterRecordAdapter.ViewHolder>() {
    companion object {
        const val TAG = "WaterRecordAdapter"
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val binding = ItemWaterRecordBinding.inflate(LayoutInflater.from(parent.context))
        return ViewHolder(binding)
    }


    override fun onBindViewHolder(holder: ViewHolder, @SuppressLint("RecyclerView") position: Int) {
        val data = datas[position]
        Log.d(TAG, "onBindViewHolder: $data")
        holder.binding.stnm.text = data.stnm
        holder.binding.location.text = StringUtils.getNoNullData(data.location)
        holder.binding.inputTime.text = data.inputTime
        holder.binding.state.text = data.state
        if( holder.binding.state.text.toString().contains("待")){
            holder.binding.state.setTextColor(Color.parseColor("#EB4141"))
        }
        else  if( holder.binding.state.text.toString().contains("已")){
            holder.binding.state.setTextColor(Color.parseColor("#32cd32"))
        }
        else{
            holder.binding.state.setTextColor(Color.parseColor("#ffa500"))
        }




        holder.binding.root.setOnClickListener {
            listener?.invoke(data)
        }


    }

    override fun getItemCount() = datas.size

    class ViewHolder(val binding: ItemWaterRecordBinding) :
        RecyclerView.ViewHolder(binding.root)
}